package com.anhry.www.safety.manage.zczb.struts.action;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.zczb.bean.TbZczbBgd;
import com.anhry.www.safety.manage.zczb.bean.TbZczbBzhInfoView;
import com.anhry.www.safety.manage.zczb.bean.TbZczbBzhTable;
import com.anhry.www.safety.manage.zczb.bean.TbZczbBzhType;
import com.anhry.www.safety.manage.zczb.bean.TbZczbHzd;
import com.anhry.www.safety.manage.zczb.bean.TbZczbYh;
import com.anhry.www.safety.manage.zczb.bean.TbZczbYhAllView;
import com.anhry.www.safety.manage.zczb.bean.TbZczbYhFj;
import com.anhry.www.safety.manage.zczb.bean.TbZczbYhNo;
import com.anhry.www.safety.manage.zczb.struts.base.ZczbBaseAction;
import com.anhry.www.safety.util.Constants;
import com.anhry.www.safety.util.DateFormater;
import com.anhry.www.safety.util.DateUtil;
import com.anhry.www.safety.util.Environment;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.UploadFile;
import com.anhry.www.safety.util.Util;
import com.anhry.www.safety.util.UtilMethod;

/**
 * @date:2012-10-9
 * @remark:
 * @author:dongbin
 */
@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class ZczbAction extends ZczbBaseAction{

	private static Logger logger = Logger.getLogger(ZczbAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String nowMonth;//当前月
	private String zcDate;//自查日期
	private String yhFxTime;//发现隐患时间
	private String yhQk;//隐患基本情况
	private String yhIsZg;//隐患是否整改0：未整改1：已整改
	private String yhGzTime;//隐患整改期限
	private String yhGzType;//隐患整改类型1：立即整改2：限期整改3：停业停产整顿
	private String yhWcTime;//整改完成日期
	private String yhGzZrr;//整改责任人
	private String yhZrrTel;//整改责任人电话
	//以下为重大隐患字段，后续再补
	private String yhJkcs;//监控及治理措施
	private String yhYxfw;//影响范围及危害后果
	private String yhXcyy;//隐患形成详细原因
	private String yhType;//隐患类型 1：一般隐患2：重大隐患
	private TbZczbBzhInfoView bzhInfo;//标准内容
	private String bgdPersonnel;//自查负责人
	private String back;

	private String scrollTop; //当前在页面中的位置
	private static Environment env = Environment.getInstance();
	//是否测试环境
	private boolean isTestEnv = "true".equals(env.getProperty("system.environment.test"));

	/**
	 * 自查自报首页
	 * @return
	 */
	public String index(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			//自查上报类型
			String reportType  = corp.getReportType();
			Calendar ca = Calendar.getInstance();
			int month = ca.get(Calendar.MONTH)+1;
			int day = ca.get(Calendar.DATE);
			//半月报从哪一天开始
			int startDayHalf = env.getIntProperty("corp.half.month.start");
			int middleDayHalf = env.getIntProperty("corp.half.month.middle");
			int endDayHalf = env.getIntProperty("corp.half.month.end");
			//月报从哪一天开始
			int startDayMonth = env.getIntProperty("corp.month.start");
			int endDayMonth = env.getIntProperty("corp.month.end");
			
			//半月报
			if(StringUtils.isNotBlank(reportType) && Constants.CORP_REPORT_HALF_MONTH.equals(reportType) ){
				if(day >= startDayHalf && day<= middleDayHalf){//如果当前日小于中间日期  那么应该是上半月 
					this.setNowMonth(month+"月上半月");//当前自查月
					this.setZcDate(month+"月" + startDayHalf + "日——"+month+"月" + middleDayHalf + "日");
				}else if( day >middleDayHalf ){ //下半月
					if(month == 2) {
						int year = Calendar.getInstance().get(Calendar.YEAR);
						if(year % 4 == 0) {
							endDayHalf = 29;
						} else {
							endDayHalf = 28;
						}
					}
					if(day <=endDayHalf) {
						this.setNowMonth(month+"月下半月");//当前自查月
						this.setZcDate(month+"月" + (middleDayHalf + 1) + "日——"+month+"月" + endDayHalf + "日");
					}  else {
						if(month==1){//如果是一月
							this.setNowMonth("12月下半月");
							this.setZcDate("12月" + middleDayHalf + "日——1月" + endDayHalf + "日");
						}else{
							this.setNowMonth((month-1)+"月下半月");
							this.setZcDate((month-1)+"月" + middleDayHalf + "日——"+month+"月" + endDayHalf + "日");
						}
					}
				} else if(day <= endDayHalf) {
					if(month==1){//如果是一月
						this.setNowMonth("12月下半月");
						this.setZcDate("12月" + middleDayHalf + "日——1月" + endDayHalf + "日");
					}else{
						this.setNowMonth((month-1)+"月下半月");
						this.setZcDate((month-1)+"月" + middleDayHalf + "日——"+month+"月" + endDayHalf + "日");
					}
				}
			} else { //月报
				if(month == 2) {
					int year = Calendar.getInstance().get(Calendar.YEAR);
					if(year % 4 == 0) {
						endDayMonth = 29;
					} else {
						endDayMonth = 28;
					}
				}
				if(day<startDayMonth){//如果当前日小于开始那天
					if(month==1){//如果是一月
						this.setNowMonth("12月份");
						this.setZcDate("12月" + startDayMonth + "日——1月" + endDayMonth + "日");
					}else{
						this.setNowMonth((month-1)+"月份");
						this.setZcDate((month-1)+"月" + startDayMonth + "日——"+month+"月" + endDayMonth + "日");
					}
				}else if (day >= startDayMonth){
					this.setNowMonth(month+"月份");//当前自查月
					if(month==12 && endDayMonth < startDayMonth){
						this.setZcDate("12月" + startDayMonth + "日——1月" + endDayMonth + "日");
					}else if( endDayMonth > startDayMonth){
						this.setZcDate(month+"月" + startDayMonth + "日——"+month+"月" + endDayMonth + "日");
					} else {
						this.setZcDate(month+"月" + startDayMonth + "日——"+(month+1)+"月" + endDayMonth + "日");
					}
				}
			}
			
			if("1".equals(corp.getCorpReportType())){//规模以上企业，适用自查标准自查
				this.setBack(this.utilService.getObjectBySql("select count(*) from tb_zczb_hzd t where 1=1 and t.hzd_status=2 and t.corp_id = "+corp.getId()));
				this.setResult("index");
			}else{//小微企业自查页面
				this.setBack(this.utilService.getObjectBySql("select count(*) from tb_zczb_xw_hzd t where 1=1 and t.hzd_status=2 and t.corp_id = "+corp.getId()));
				this.setResult("xw_index");
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction index() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 当前半月报告单列表
	 * @return
	 */
	public String bgdList(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			String year = UtilMethod.getZcYear();
		
			String reportType = corp.getReportType();
			String month = UtilMethod.getZcMonth();
			if(StringUtils.isNotBlank(reportType) && Constants.CORP_REPORT_HALF_MONTH.equals(reportType)) {
				int currentHalf= DateUtil.currentReportPeriod();
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbBgd t where 1=1 and t.corpId="+corp.getId()+
						" and t.bgdYear="+year + " and t.bgdMonth="+month+" and t.bgdHalf =" + currentHalf + "order by t.bgdNum"));
			} else {
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbBgd t where 1=1 and t.corpId="+corp.getId()+" and t.bgdYear="+year+" and t.bgdMonth="+month+" order by t.bgdNum"));
			}
			
			this.setResult("bgd_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction bgdList() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 新建自查
	 * @return
	 */
	public String toZc(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			TbZczbBzhTable tab = null;
			if(null!=corp.getTbCorpTypeMin().getHiddenTabId()){
				tab = (TbZczbBzhTable)this.utilService.getObjectById(TbZczbBzhTable.class, corp.getTbCorpTypeMin().getHiddenTabId());
				if(null!=tab){
					this.setTab(tab);
				}
			}
			String tem = this.typeTreeInit(tab).asXML();
			tem = tem.replaceAll("\\\"", "\\\\\"");
			tem = tem.replaceAll("\n", "");
			this.setItemXml(tem);
			if(null==this.hzdId || "".equals(this.hzdId)){
				List list = null;
				//半月报企业
				if(StringUtils.isNotBlank(corp.getReportType()) && Constants.CORP_REPORT_HALF_MONTH.equals(corp.getReportType())) {
					list = (List)this.utilService.getHqlQuery("from TbZczbHzd t where 1=1 " +
							"and t.hzdYear="+UtilMethod.getZcYear()+" and t.hzdMonth="+ UtilMethod.getZcMonth()+ "and t.hzdHalf = "+ DateUtil.currentReportPeriod() + " and t.corpId="+corp.getId());
				} else {
					list = (List)this.utilService.getHqlQuery("from TbZczbHzd t where 1=1 " +
							"and t.hzdYear="+UtilMethod.getZcYear()+" and t.hzdMonth="+UtilMethod.getZcMonth()+" and t.corpId="+corp.getId());
				}
				
				if(null!=list && list.size()>0){
					TbZczbHzd hzd = (TbZczbHzd)list.get(0);
					this.setHzdId(hzd.getId().toString());
				}
			}
			if(null!=this.bgdId && this.bgdId.length()>0){
				TbZczbBgd bgd = (TbZczbBgd)this.utilService.getObjectById(TbZczbBgd.class, Long.parseLong(this.bgdId));
				this.setBgd(bgd);
			}
			this.setResult("bgd_add");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction toZc() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 取得自查标准列表
	 * @return
	 */
	public String bzhList(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			if(null!=corp.getTbCorpTypeMin().getHiddenTabId()){
				TbZczbBzhTable tab = (TbZczbBzhTable)this.utilService.getObjectById(TbZczbBzhTable.class, corp.getTbCorpTypeMin().getHiddenTabId());
				if(null!=tab){
					StringBuffer sbf = new StringBuffer("from TbZczbBzhInfoView t where 1=1 and t.tabId="+tab.getId()+" ");
					this.setTab(tab);
					if(null!=this.cType && this.cType.length()>0){
						if("1".equals(this.cType) || "2".equals(this.cType)){
							sbf.append(" and t.infoType1="+this.cType);
						}else{
							sbf.append(" and t.infoType2="+this.cType);
						}
					}else{//默认查询现场管理数据
						sbf.append(" and t.infoType1=1");
					}
					sbf.append(" order by t.infoSort");
					List list = (List)this.utilService.getHqlQuery(sbf.toString());
					this.setObjList(this.initBzh(list, this.bgdId, this.cType));
				}
			}
			this.setResult("bzh_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction bzhList() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 无隐患保存
	 * @return
	 */
	public String saveYhno(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			if(null!=this.bzhId && this.bzhId.length()>0){
				TbZczbBzhInfoView info = (TbZczbBzhInfoView)this.utilService.getObjectById(TbZczbBzhInfoView.class, Long.parseLong(this.bzhId));
				if(null!=info){
					TbZczbYhNo yh = new TbZczbYhNo();
					yh.setYhType1(info.getInfoType1());
					yh.setYhType1Name(info.getInfoType1Name());
					yh.setYhType2(info.getInfoType2());
					yh.setYhType2Name(info.getInfoType2Name());
					yh.setYhType3(info.getInfoType3());
					yh.setYhType4(info.getInfoType4());
					yh.setYhInfoId(info.getId());
					yh.setYhInfo(info.getInfoContent());
					yh.setYhYj(info.getInfoYj());
					yh.setTabId(info.getTabId());
					yh.setTabName(info.getTabName());
					yh.setCorpId(corp.getId());
					yh.setHzdStatus("1");
					yh.setYhDate(new Date());
					if(null!=this.bgdId && this.bgdId.length()>0){
						yh.setBgdId(Long.parseLong(this.bgdId));
					}
					this.utilService.save(yh);
					this.initBgd(this.bgdId);//如果报告单不为空，怎更新报告单和汇总单
					return this.bzhList();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction saveYhno() 异常："+e.getMessage());
		}
		this.setTip("该隐患标准不存在！");
		return ERROR;
	}
	
	/**
	 * 跳转到隐患添加/修改页面 一般隐患和重大隐患
	 * @return
	 */
	public String yhEdit(){
		try {
			if(null!=this.yhId && this.yhId.length()>0){
				TbZczbYh yh = (TbZczbYh)this.utilService.getObjectById(TbZczbYh.class, Long.parseLong(this.yhId));
				if(null!=yh){
					this.setYh(yh);
					this.setFjList1(this.zczbService.getYhFjList("1",this.yhId));
					this.setFjList2(this.zczbService.getYhFjList("2",this.yhId));
					this.setFjList3(this.zczbService.getYhFjList("3",this.yhId));
					this.setResult("yh_edit");
					return SUCCESS;
				}
			}else{
				if(null!=this.bzhId && this.bzhId.length()>0){
					TbZczbBzhInfoView info = (TbZczbBzhInfoView)this.utilService.getObjectById(TbZczbBzhInfoView.class, Long.parseLong(this.bzhId));
					if(null!=info){
						this.setBzhInfo(info);
						this.setResult("yh_add");
						return SUCCESS;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction yhAdd() 异常："+e.getMessage());
		}
		this.setTip("该隐患标准不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患保存
	 * @return
	 */
	public String saveYh(){
		try {
			if(null!=this.yhId && this.yhId.length()>0){
				TbZczbYh yh = (TbZczbYh)this.utilService.getObjectById(TbZczbYh.class, Long.parseLong(this.yhId));
				if(null!=yh){
					yh = this.yhInit(yh);
					this.utilService.update(yh);
					this.initBgd(this.bgdId);//如果报告单不为空，怎更新报告单和汇总单
					return this.saveYhfj(yh);
				}
			}else{
				if(null!=this.bzhId && this.bzhId.length()>0){
					TbZczbYh yh = new TbZczbYh();
					yh = this.yhInit(yh);
					yh.setHzdStatus("1");
					yh.setYhCome("1");
					yh.setYhStatus(1);
					yh.setYhIsGp("0");
					yh.setYhIsXh("0");
					yh.setYhDate(new Date());
					if(null!=this.bgdId && this.bgdId.length()>0){
						yh.setBgdId(Long.parseLong(this.bgdId));
					}
					this.utilService.save(yh);
					this.initBgd(this.bgdId);//如果报告单不为空，怎更新报告单和汇总单
					return this.saveYhfj(yh);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction saveYhno() 异常："+e.getMessage());
		}
		this.setTip("该隐患标准不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患附件保存
	 * @param yh
	 */
	public String saveYhfj(TbZczbYh yh){
		try {
			if(null!=this.yhFile && this.yhFile.length>0){
				for(int i=0;i<this.yhFile.length;i++){
					if(logger.isDebugEnabled()) {
						logger.debug("Upload picture type is : " +this.yhFileContentType[i]);
					}
					if(!UtilMethod.filterType(this.yhFileContentType[i], Util.ALLOWTYPES.split(","))){
						this.setTip("图片附件格式错误,请重新上传！");
						return this.yhEdit();
					}
				}
				for(int i=0;i<this.yhFile.length;i++){
					TbZczbYhFj fj = new TbZczbYhFj();
					fj.setFjTitle(this.yhFileFileName[i]);
					/*String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB + File.separator + DateUtil.getDateStr();
					String fileName = UploadFile.copy2(this.yhFile[i], tem_path.replaceAll("\\\\", "/"), this.yhFileFileName[i]);
					String path = Util.FILE_FOLDER+File.separator+Util.ZCZB+File.separator+ DateUtil.getDateStr()+fileName;
					fj.setFjPath(path.replaceAll("\\\\", "/"));*/
					
					uploadAttachment(fj, yhFile[i], yhFileFileName[i]);
					fj.setIsDele("0");
					fj.setFjType("1");
					fj.setYhId(yh.getId());
					this.utilService.save(fj);
				}
			}
			if(null!=this.picFile && this.picFile.length>0){
				for(int i=0;i<this.picFile.length;i++){
					if(!UtilMethod.filterType(this.picFileContentType[i], Util.ALLOWTYPES.split(","))){
						this.setTip("图片附件格式错误,请重新上传！");
						return this.yhEdit();
					}
				}
				for(int i=0;i<this.picFile.length;i++){
					TbZczbYhFj fj = new TbZczbYhFj();
					fj.setFjTitle(this.picFileFileName[i]);
				/*	String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB;
					String fileName = UploadFile.copy2(this.picFile[i], tem_path, this.picFileFileName[i]);
					String path = Util.FILE_FOLDER+File.separator+Util.ZCZB+File.separator+fileName;
					fj.setFjPath(path);*/
					uploadAttachment(fj, picFile[i], picFileFileName[i]);
					fj.setIsDele("0");
					fj.setFjType("2");
					fj.setYhId(yh.getId());
					this.utilService.save(fj);
				}
			}
			if(null!=this.docFile && this.docFile.length>0){
				for(int i=0;i<this.docFile.length;i++){
					TbZczbYhFj fj = new TbZczbYhFj();
					fj.setFjTitle(this.docFileFileName[i]);
				/*	String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB;
					String fileName = UploadFile.copy2(this.docFile[i], tem_path, this.docFileFileName[i]);
					String path = Util.FILE_FOLDER+File.separator+Util.ZCZB+File.separator+fileName;
					fj.setFjPath(path);*/
					uploadAttachment(fj, docFile[i], docFileFileName[i]);
					fj.setIsDele("0");
					fj.setFjType("3");
					fj.setYhId(yh.getId());
					this.utilService.save(fj);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction saveYhfj(TbZczbYh yh) 异常："+e.getMessage());
		}
		this.setTip("操作成功！");
		return this.bzhList();
	}
	
	private void uploadAttachment(TbZczbYhFj fj, File file, String fname) {
		String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB + File.separator + DateUtil.getDateStr();
		String fileName = UploadFile.copy2(file, tem_path.replaceAll("\\\\", "/"), fname);
		String path = Util.FILE_FOLDER+File.separator+Util.ZCZB+File.separator+ DateUtil.getDateStr()+fileName;
		fj.setFjPath(path.replaceAll("\\\\", "/"));
	}
	
	/**
	 * 异步删除附件
	 *
	 */
	public void deleFj(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if(null!=this.getFjId() && !"".equals(this.getFjId())){
				TbZczbYhFj fj = (TbZczbYhFj)this.utilService.getObjectById(TbZczbYhFj.class, Long.parseLong(this.fjId));
				if(null!=fj){
					UploadFile.removeFile(Util.SAFETY_FILE + File.separator + fj.getFjPath());
					this.utilService.delete(fj);
				}
				this.contextPvd.getResponse().getWriter().write("1");
			}else{
				this.contextPvd.getResponse().getWriter().write("2");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction deleFj() 异常："+e.getMessage());
		}
	}
	
	/**
	 * 隐患信息保存
	 * @param yhType 隐患类型 1：一般隐患2：重大隐患
	 */
	public TbZczbYh yhInit(TbZczbYh yh){
		TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		try {
			yh.setCorpId(corp.getId());
			
			yh.setYhQk(this.yhQk);
			yh.setYhFxTime(DateFormater.formatDate(this.yhFxTime));
			if(null!=this.yhGzTime && this.yhGzTime.length()>0){
				yh.setYhZgTime(DateFormater.formatDate(yhGzTime));
			}
			yh.setYhZgType(this.yhGzType);
			if(null!=this.yhWcTime && this.yhWcTime.length()>0){
				yh.setYhWcTime(DateFormater.formatDate(this.yhWcTime));
			}
			yh.setYhZgZrr(this.yhGzZrr);
			yh.setYhZrrTel(this.yhZrrTel);
			yh.setYhIsZg(this.yhIsZg);
			
			if(null!=this.bzhId && this.bzhId.length()>0){
				TbZczbBzhInfoView info = (TbZczbBzhInfoView)this.utilService.getObjectById(TbZczbBzhInfoView.class, Long.parseLong(this.bzhId));
				if(null!=info){
					yh.setYhType1(info.getInfoType1());
					yh.setYhType1Name(info.getInfoType1Name());
					yh.setYhType2(info.getInfoType2());
					yh.setYhType2Name(info.getInfoType2Name());
					yh.setYhType3(info.getInfoType3());
					yh.setYhType4(info.getInfoType4());
					yh.setYhInfoId(info.getId());
					yh.setYhInfo(info.getInfoContent());
					yh.setYhYj(info.getInfoYj());
					yh.setTabId(info.getTabId());
					yh.setTabName(info.getTabName());
				}
			}
			yh.setYhType(this.yhType);//隐患类型 
			yh.setYhJkcs(this.yhJkcs);
			yh.setYhXcyy(this.yhXcyy);
			yh.setYhYxfw(this.yhYxfw);
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction TbZczbYh yhInit(TbZczbYh yh)异常："+e.getMessage());
		}
		return yh;
	}
	
	public String deleYh(){
		try {
			if(null!=this.yhId && this.yhId.length()>0){
				if("0".equals(this.yhType)){
					this.utilService.deleteBySql("delete tb_zczb_yh_no t where t.id="+this.yhId);
				}else{
					this.utilService.deleteBySql("delete tb_zczb_yh t where t.id="+this.yhId);
					this.utilService.deleteBySql("delete tb_zczb_yh_fj t where t.yh_id="+this.yhId);
				}
				this.initBgd(this.bgdId);
				return this.bzhList();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction TbZczbYh yhInit(TbZczbYh yh)异常："+e.getMessage());
		}
		this.setTip("你删除的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 检测是否已经自查，如没有自查报告单不能保存
	 */
	public void bgdCheck(){
		this.contextPvd.getResponse().setHeader("Cache-Control","no-cache");
		this.contextPvd.getResponse().setCharacterEncoding("utf-8");
		TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		try {
			String count = "0";
			if(null!=this.bgdId && this.bgdId.length()>0){
				count = "1";
			}else{
				count = this.utilService.getObjectBySql("select count(*) from tb_zczb_yh_all_view t where 1=1 and t.bgd_id is null and t.corp_id="+corp.getId());
			}
			this.contextPvd.getResponse().getWriter().write(count);
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			Loggers.error("zczbAction bgdCheck()异常："+e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 报告单保存
	 * @return
	 */
	public String saveBgd(){
		TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		TbZczbHzd hzd1 = null;
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				hzd1 = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
			}
			if(null!=this.bgdId && this.bgdId.length()>0){
				TbZczbBgd bgd = (TbZczbBgd)this.utilService.getObjectById(TbZczbBgd.class, Long.parseLong(this.bgdId));
				if(null!=bgd){
					bgd = this.initBgd(bgd, bgdId);
					bgd.setBgdPersonnel(this.bgdPersonnel);
					this.utilService.update(bgd);
					this.initHzd();
					this.setTip("保存成功！");
					if(null!=hzd1 && "2".equals(hzd1.getHzdStatus())){//如果是打回的汇总单
						return this.backBgdList();
					}
					return this.bgdList();
				}
			}else{
				TbZczbBgd bgd = new TbZczbBgd();
				bgd = this.initBgd(bgd, bgdId);
				if(null!=this.hzdId && this.hzdId.length()>0){
					bgd.setBgdYear(hzd1.getHzdYear());
					bgd.setBgdMonth(hzd1.getHzdMonth());
					bgd.setBgdNum(Integer.parseInt(this.utilService.getObjectBySql("select max(t.bgd_num) from tb_zczb_bgd t where 1=1 " +
							"and t.bgd_year="+hzd1.getHzdYear()+" and t.bgd_month="+hzd1.getHzdMonth()+" and t.corp_id="+corp.getId()))+1);
				}else{
					bgd.setBgdYear(Integer.parseInt(UtilMethod.getZcYear()));
					bgd.setBgdMonth(Integer.parseInt(UtilMethod.getZcMonth()));
					bgd.setBgdNum(Integer.parseInt(this.utilService.getObjectBySql("select max(t.bgd_num) from tb_zczb_bgd t where 1=1 " +
							"and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth()+" and t.corp_id="+corp.getId()))+1);
				}
				bgd.setBgdPersonnel(this.bgdPersonnel);
				bgd.setBgdSaveTime(new Date());
				bgd.setCorpId(corp.getId());
				bgd.setIsDele("0");
				//上报类型为半月报的 设置是上半月还是下半月
				if(StringUtils.isNotBlank(corp.getReportType()) && Constants.CORP_REPORT_HALF_MONTH.equals(corp.getReportType())) {
					bgd.setBgdHalf(DateUtil.currentReportPeriod());
				}
				this.utilService.saveOrUpdate(bgd);
				this.utilService.deleteBySql("update tb_zczb_yh_no t set t.bgd_id="+bgd.getId()+" where t.bgd_id is null and t.corp_id="+corp.getId());
				this.utilService.deleteBySql("update tb_zczb_yh t set t.bgd_id="+bgd.getId()+" where t.bgd_id is null and t.corp_id="+corp.getId());
				//更新汇总单
				TbZczbHzd hzd = this.initHzd();
				bgd.setHzdId(hzd.getId());
				this.utilService.update(bgd);
				this.setTip("保存成功！");
				if(null!=hzd1 && "2".equals(hzd1.getHzdStatus())){
					return this.backBgdList();
				}
				return this.bgdList();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction saveBgd()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	
	/**
	 * 报告单信息保存
	 * @param bgd
	 * @param bgdId
	 * @return
	 */
	public TbZczbBgd initBgd(TbZczbBgd bgd,String bgdId){
		TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		int itemNum = 0;	//自查总项数
		int bigNum = 0;		//重大隐患数
		int sameNum = 0;	//一般隐患数
		int sameClNum = 0;	//一般隐患数处理数
		int bigClNum = 0;	//重大隐患数处理数
		StringBuilder sql = new StringBuilder("select count(*) from tb_zczb_yh_all_view t where 1=1 and t.corp_id="+corp.getId());
		String sql0 = "";
		String sql1 = "";
		String sql2 = "";
		String sql3 = "";
		String sql4 = "";
		try {
			if(null!=this.bgdId && this.bgdId.length()>0){
				sql0 = sql.toString()+(" and t.bgd_id="+this.bgdId);//一般隐患
				sql1 = sql.toString()+(" and t.yh_type=1 and t.bgd_id="+this.bgdId);//一般隐患
				sql2 = sql.toString()+(" and t.yh_type=2 and t.bgd_id="+this.bgdId);//重大隐患
				sql3 = sql.toString()+(" and t.yh_type=1 and t.yh_is_zg=1 and t.bgd_id="+this.bgdId);//一般隐患处理数
				sql4 = sql.toString()+(" and t.yh_type=2 and t.yh_is_zg=1 and t.bgd_id="+this.bgdId);//重大隐患处理数
			}else{
				sql0 = sql.toString()+(" and t.bgd_id is null");//一般隐患
				sql1 = sql.toString()+(" and t.yh_type=1 and t.bgd_id is null");//一般隐患
				sql2 = sql.toString()+(" and t.yh_type=2 and t.bgd_id is null");//重大隐患
				sql3 = sql.toString()+(" and t.yh_type=1 and t.yh_is_zg=1 and t.bgd_id is null");//一般隐患处理数
				sql4 = sql.toString()+(" and t.yh_type=2 and t.yh_is_zg=1 and t.bgd_id is null");//重大隐患处理数
			}
			itemNum = Integer.parseInt(this.utilService.getObjectBySql(sql0));
			sameNum = Integer.parseInt(this.utilService.getObjectBySql(sql1));
			bigNum = Integer.parseInt(this.utilService.getObjectBySql(sql2));
			sameClNum = Integer.parseInt(this.utilService.getObjectBySql(sql3));
			bigClNum = Integer.parseInt(this.utilService.getObjectBySql(sql4));
			bgd.setBgdItemNum(itemNum);
			bgd.setBgdSameNum(sameNum);
			bgd.setBgdBigNum(bigNum);
			bgd.setBgdYhNum(sameNum+bigNum);
			bgd.setBgdSameCl(sameClNum);
			bgd.setBgdBigCl(bigClNum);
			bgd.setBgdClNum(sameClNum+bigClNum);
			bgd.setBgdWclNum(sameNum+bigNum-sameClNum-bigClNum);
			
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction initBgd() 异常："+e.getMessage());
		}
		return bgd;
	}
	
	public void initBgd(String bgdId){
		TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		int itemNum = 0;	//自查总项数
		int bigNum = 0;		//重大隐患数
		int sameNum = 0;	//一般隐患数
		int sameClNum = 0;	//一般隐患数处理数
		int bigClNum = 0;	//重大隐患数处理数
		StringBuilder sql = new StringBuilder("select count(*) from tb_zczb_yh_all_view t where 1=1 and t.corp_id="+corp.getId());
		String sql0 = "";
		String sql1 = "";
		String sql2 = "";
		String sql3 = "";
		String sql4 = "";
		try {
			if(null!=this.bgdId && this.bgdId.length()>0){
				TbZczbBgd bgd = (TbZczbBgd)this.utilService.getObjectById(TbZczbBgd.class, Long.parseLong(this.bgdId));
				sql0 = sql.toString()+(" and t.bgd_id="+this.bgdId);//一般隐患
				sql1 = sql.toString()+(" and t.yh_type=1 and t.bgd_id="+this.bgdId);//一般隐患
				sql2 = sql.toString()+(" and t.yh_type=2 and t.bgd_id="+this.bgdId);//重大隐患
				sql3 = sql.toString()+(" and t.yh_type=1 and t.yh_is_zg=1 and t.bgd_id="+this.bgdId);//一般隐患处理数
				sql4 = sql.toString()+(" and t.yh_type=2 and t.yh_is_zg=1 and t.bgd_id="+this.bgdId);//重大隐患处理数
				itemNum = Integer.parseInt(this.utilService.getObjectBySql(sql0));
				sameNum = Integer.parseInt(this.utilService.getObjectBySql(sql1));
				bigNum = Integer.parseInt(this.utilService.getObjectBySql(sql2));
				sameClNum = Integer.parseInt(this.utilService.getObjectBySql(sql3));
				bigClNum = Integer.parseInt(this.utilService.getObjectBySql(sql4));
				bgd.setBgdItemNum(itemNum);
				bgd.setBgdSameNum(sameNum);
				bgd.setBgdBigNum(bigNum);
				bgd.setBgdYhNum(sameNum+bigNum);
				bgd.setBgdSameCl(sameClNum);
				bgd.setBgdBigCl(bigClNum);
				bgd.setBgdClNum(sameClNum+bigClNum);
				bgd.setBgdWclNum(sameNum+bigNum-sameClNum-bigClNum);
				this.utilService.update(bgd);
				this.initHzd();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction void initBgd() 异常："+e.getMessage());
		}
	}
	
	/**
	 * 删除报告单
	 *
	 */
	public void deleBgd(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if(null!=this.getBgdId() && !"".equals(this.getBgdId())){
				TbZczbBgd bgd = (TbZczbBgd)this.utilService.getObjectById(TbZczbBgd.class, Long.parseLong(this.bgdId));
				if(null!=bgd){
					if(bgd.getBgdBigNum()==0){
						this.setHzdId(bgd.getHzdId().toString());
						this.utilService.deleteBySql("delete tb_zczb_yh_fj t where t.yh_id in(select x.id from tb_zczb_yh x where x.bgd_id="+bgd.getId()+")");
						this.utilService.deleteBySql("delete tb_zczb_yh t where t.bgd_id="+bgd.getId());
						this.utilService.deleteBySql("delete tb_zczb_yh_no t where t.bgd_id="+bgd.getId());
						this.utilService.delete(bgd);
						this.initHzd();
						this.contextPvd.getResponse().getWriter().write("1");
					}else{
						this.contextPvd.getResponse().getWriter().write("2");
					}
				}else{
					this.contextPvd.getResponse().getWriter().write("3");
				}
			}else{
				this.contextPvd.getResponse().getWriter().write("3");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction deleFj() 异常："+e.getMessage());
		}
	}
	
	/**
	 * 报告单信息保存
	 * @param bgd
	 * @param bgdId
	 * @return
	 */
	public TbZczbHzd initHzd(){
		TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		TbZczbHzd hzd = null;
		StringBuilder sql = new StringBuilder("select count(*) from tb_zczb_bgd t where 1=1 ");//自查次数
		StringBuilder sql0 = new StringBuilder("select sum(t.bgd_item_num) from tb_zczb_bgd t where 1=1 ");//自查项数
		StringBuilder sql1 = new StringBuilder("select sum(t.bgd_same_num) from tb_zczb_bgd t where 1=1 ");//一般隐患数
		StringBuilder sql2 = new StringBuilder("select sum(t.bgd_big_num) from tb_zczb_bgd t where 1=1 ");//重大隐患数
		StringBuilder sql3 = new StringBuilder("select sum(t.bgd_same_cl) from tb_zczb_bgd t where 1=1 ");//一般隐患处理数
		StringBuilder sql4 = new StringBuilder("select sum(t.bgd_big_cl) from tb_zczb_bgd t where 1=1 ");//重大隐患处理数
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				
				if(StringUtils.isNotBlank(corp.getReportType()) && Constants.CORP_REPORT_HALF_MONTH.equals(corp.getReportType())) {
					sql.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth()+" and t.bgd_half=" + hzd.getHzdHalf());//自查次数
					sql0.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth()+" and t.bgd_half=" + hzd.getHzdHalf());//自查项数
					sql1.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth()+" and t.bgd_half=" + hzd.getHzdHalf());//一般隐患数
					sql2.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth()+" and t.bgd_half=" + hzd.getHzdHalf());//重大隐患数
					sql3.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth()+" and t.bgd_half=" + hzd.getHzdHalf());//一般隐患处理数
					sql4.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth()+" and t.bgd_half=" + hzd.getHzdHalf());//重大隐患处理数
				} else {
					sql.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth());//自查次数
					sql0.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth());//自查项数
					sql1.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth());//一般隐患数
					sql2.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth());//重大隐患数
					sql3.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth());//一般隐患处理数
					sql4.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+hzd.getHzdYear()+" and t.bgd_month="+hzd.getHzdMonth());//重大隐患处理数
				}
				
			}else{
				hzd = new TbZczbHzd();
				if(StringUtils.isNotBlank(corp.getReportType()) && Constants.CORP_REPORT_HALF_MONTH.equals(corp.getReportType())) {
					sql.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth()+" and t.bgd_half=" + DateUtil.currentReportPeriod());//自查次数
					sql0.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth()+" and t.bgd_half=" +  DateUtil.currentReportPeriod());//自查项数
					sql1.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth()+" and t.bgd_half=" +  DateUtil.currentReportPeriod());//一般隐患数
					sql2.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth()+" and t.bgd_half=" +  DateUtil.currentReportPeriod());//重大隐患数
					sql3.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth()+" and t.bgd_half=" +  DateUtil.currentReportPeriod());//一般隐患处理数
					sql4.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth()+" and t.bgd_half=" +  DateUtil.currentReportPeriod());//重大隐患处理数
					hzd.setHzdHalf(DateUtil.currentReportPeriod());
				} else {
					sql.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth());//自查次数
					sql0.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth());//自查项数
					sql1.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth());//一般隐患数
					sql2.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth());//重大隐患数
					sql3.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth());//一般隐患处理数
					sql4.append(" and t.corp_id="+corp.getId()+" and t.bgd_year="+UtilMethod.getZcYear()+" and t.bgd_month="+UtilMethod.getZcMonth());//重大隐患处理数
				}
			
				hzd.setHzdYear(Integer.parseInt(UtilMethod.getZcYear()));
				hzd.setHzdMonth(Integer.parseInt(UtilMethod.getZcMonth()));
				hzd.setHzdHalf(DateUtil.currentReportPeriod());
				hzd.setHzdSaveTime(new Date());
				if(isTestEnv) {
					hzd.setHzdStatus("1");
				} else {
					hzd.setHzdStatus("0");
				}
				
				hzd.setIsDele("0");
				hzd.setHzdLooks(0);
				hzd.setHzdChecks(0);
				hzd.setCorpId(corp.getId());
				this.utilService.save(hzd);
			}
			if(null!=hzd){
				if(Integer.parseInt(this.utilService.getObjectBySql(sql.toString()))==0 && "0".equals(hzd.getHzdStatus())){//如果报告单为0，并且汇总单状态为为提交状态
					this.utilService.delete(hzd);
				}else{
					hzd.setHzdBgdNum(Integer.parseInt(this.utilService.getObjectBySql(sql.toString())));
					hzd.setHzdItemNum(Integer.parseInt(this.utilService.getObjectBySql(sql0.toString())));
					hzd.setHzdSameNum(Integer.parseInt(this.utilService.getObjectBySql(sql1.toString())));
					hzd.setHzdBigNum(Integer.parseInt(this.utilService.getObjectBySql(sql2.toString())));
					hzd.setHzdYhNum(hzd.getHzdSameNum()+hzd.getHzdBigNum());
					hzd.setHzdSameCl(Integer.parseInt(this.utilService.getObjectBySql(sql3.toString())));
					hzd.setHzdBigCl(Integer.parseInt(this.utilService.getObjectBySql(sql4.toString())));
					hzd.setHzdClNum(hzd.getHzdSameCl()+hzd.getHzdBigCl());
					hzd.setHzdWclNum(hzd.getHzdSameNum()+hzd.getHzdBigNum()-hzd.getHzdSameCl()-hzd.getHzdBigCl());
					this.utilService.update(hzd);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction initHzd() 异常："+e.getMessage());
		}
		return hzd;
	}
	
	/**
	 * 初始化报告单
	 * @param bgdId  报告单id
	 * @param yhType 隐患类型 0：无隐患1：一般隐患2：重大隐患
	 * @param isZg 是否整改 0:未整改 1：已整改
	 * @return
	 */
//	public TbZczbBgd resetBgd(String bgdId,String yhType,String isZg){
//		TbZczbBgd bgd = null;
//		TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
//		try {
//			if(null!=bgdId && bgdId.length()>0){//如果报告单存在
//				bgd = (TbZczbBgd)this.utilService.getObjectById(TbZczbBgd.class, Long.parseLong(bgdId));
//				bgd.setBgdItemNum(bgd.getBgdItemNum()+1);
//				if("1".equals(yhType)){//一般隐患
//					bgd.setBgdSameNum(bgd.getBgdSameNum()+1);
//					bgd.setBgdYhNum(bgd.getBgdYhNum()+1);
//				}
//				if("2".equals(yhType)){//重大隐患
//					bgd.setBgdBigNum(bgd.getBgdBigNum()+1);
//					bgd.setBgdYhNum(bgd.getBgdYhNum()+1);
//				}
//				if("0".equals(isZg)){
//					bgd.setBgdWclNum(bgd.getBgdWclNum()+1);
//				}else if("1".equals(isZg)){
//					if("1".equals(yhType)){//一般隐患
//						bgd.setBgdSameCl(bgd.getBgdSameCl()+1);
//					}
//					if("2".equals(yhType)){//重大隐患
//						bgd.setBgdBigCl(bgd.getBgdBigCl()+1);
//					}
//					bgd.setBgdClNum(bgd.getBgdClNum()+1);
//				}
//				this.utilService.update(bgd);
//			}else{
//				bgd = new TbZczbBgd();
//				bgd.setBgdItemNum(1);
//				if("1".equals(yhType)){//一般隐患
//					bgd.setBgdSameNum(1);
//					bgd.setBgdYhNum(1);
//					bgd.setBgdBigNum(0);
//					bgd.setBgdYhNum(0);
//				}else if("2".equals(yhType)){
//					bgd.setBgdBigNum(1);
//					bgd.setBgdYhNum(1);
//					bgd.setBgdSameNum(0);
//					bgd.setBgdYhNum(0);
//				}
//				if("0".equals(isZg)){//未处理
//					bgd.setBgdWclNum(1);
//				}else if("1".equals(isZg)){
//					if("1".equals(yhType)){//一般隐患
//						bgd.setBgdSameCl(1);
//						bgd.setBgdBigCl(0);
//					}else if("2".equals(yhType)){
//						bgd.setBgdSameCl(0);
//						bgd.setBgdBigCl(1);
//					}
//					bgd.setBgdClNum(1);
//				}
//				
//				bgd.setBgdYear(Integer.parseInt(UtilMethod.getZcYear()));
//				bgd.setBgdMonth(Integer.parseInt(UtilMethod.getZcMonth()));
//				bgd.setCorpId(corp.getId());
////				bgd.setHzdId(hzdId);
//				bgd.setIsDele("0");
//				this.utilService.saveOrUpdate(bgd);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			Loggers.error("zczbAction initBgd() 异常："+e.getMessage());
//		}
//		return bgd;
//	}
	
	public List initBzh(List<TbZczbBzhInfoView> infoList,String bgdId,String cType){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			List<TbZczbYhAllView> yhList = this.zczbService.getYhList(corp.getId().toString(),bgdId,cType);
			if(null!=infoList && infoList.size()>0 && null!=yhList && yhList.size()>0){
				for(TbZczbBzhInfoView view : infoList){
					for(TbZczbYhAllView yh : yhList){
						if(yh.getYhInfoId().toString().equals(view.getId().toString())){
							view.setYhId(yh.getId());
							view.setYhType(yh.getYhType());
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction List initBzh(List<TbZczbBzhInfoView> infoList,String bgdId)异常："+e.getMessage());
		}
		return infoList;
	}
	
	/**
	 * 隐患详细信息
	 * @return
	 */
	public String yhShow(){
		try {
			if(null!=this.yhId && this.yhId.length()>0){
				TbZczbYh yh = (TbZczbYh)this.utilService.getObjectById(TbZczbYh.class, Long.parseLong(this.yhId));
				if(null!=yh){
					this.setYh(yh);
					this.setFjList1(this.zczbService.getYhFjList("1",this.yhId));
					this.setFjList2(this.zczbService.getYhFjList("2",this.yhId));
					this.setFjList3(this.zczbService.getYhFjList("3",this.yhId));
					this.setResult("yh_show");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction yhShow()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 打回列表
	 * @return
	 */
	public String backList(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			StringBuilder hql = new StringBuilder("from TbZczbHzd t where 1=1 and t.hzdStatus=2 and t.corpId="+corp.getId());
			if(null!=this.cYear && this.cYear.length()>0){
				hql.append(" and t.hzdYear="+this.cYear);
			}else{
				hql.append(" and t.hzdYear="+year);
				this.setCYear(String.valueOf(year));
			}
			hql.append("order by t.hzdMonth");
			this.setObjList((List)this.utilService.getHqlQuery(hql.toString()));
			this.setYearList(UtilMethod.getYearList(2012));
			this.setResult("back_hzd_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction backList() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 汇总单重新上报
	 *
	 */
	public void backUp(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if(null!=this.getHzdId() && !"".equals(this.getHzdId())){
				TbZczbHzd hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd && hzd.getHzdBgdNum()>0){
					hzd.setHzdStatus("1");//状态修改为1
					hzd.setHzdBackDate(null);
					hzd.setHzdBackLy(null);
					hzd.setHzdBackUser(null);
					this.utilService.update(hzd);
					//将所有隐患信息状态修改为已上报 
					this.utilService.deleteBySql("update tb_zczb_yh t set t.hzd_status=1 where t.bgd_id in(select x.id from tb_zczb_bgd x where x.hzd_id="+this.hzdId+")");
					this.utilService.deleteBySql("update tb_zczb_yh_no t set t.hzd_status=1 where t.bgd_id in(select x.id from tb_zczb_bgd x where x.hzd_id="+this.hzdId+")");
					this.contextPvd.getResponse().getWriter().write("1");
				}else{
					this.contextPvd.getResponse().getWriter().write("2");
				}
			}else{
				this.contextPvd.getResponse().getWriter().write("3");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction deleFj() 异常："+e.getMessage());
		}
	}
	
	/**
	 * 报告单列表
	 * @return
	 */
	public String backBgdList(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbBgd t where 1=1 and t.hzdId="+this.hzdId+" order by t.bgdNum"));
				this.setResult("back_bgd_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("HistoryAction bgdList() 异常："+e.getMessage());
		}
		this.setTip("数据不存在！");
		return ERROR;
	}
	
	/**
	 * 打回记录单修改或新建自查
	 * @return
	 */
	public String toBackZc(){
		try {
			TbCorpInfo corp = (TbCorpInfo)this.contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			TbZczbBzhTable tab = null;
			if(null!=corp.getTbCorpTypeMin().getHiddenTabId()){
				tab = (TbZczbBzhTable)this.utilService.getObjectById(TbZczbBzhTable.class, corp.getTbCorpTypeMin().getHiddenTabId());
				if(null!=tab){
					this.setTab(tab);
				}
			}
			String tem = this.typeTreeInit(tab).asXML();
			tem = tem.replaceAll("\\\"", "\\\\\"");
			tem = tem.replaceAll("\n", "");
			this.setItemXml(tem);
			if(null!=this.bgdId && this.bgdId.length()>0){
				TbZczbBgd bgd = (TbZczbBgd)this.utilService.getObjectById(TbZczbBgd.class, Long.parseLong(this.bgdId));
				this.setBgd(bgd);
			}
			this.setResult("back_bgd_edit");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction toBackZc() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 取得1、2级类型
	 * @return
	 */
	
	public Document typeTreeInit(TbZczbBzhTable tab){
		try {
			String t1 = "0";
			String t2 = "0";
			List tList = null;
			if(null!=tab){
				String sql1 = "select count(*) from tb_zczb_bzh_info t where t.tab_id="+tab.getId()+" and t.info_type1=1";
				String sql2 = "select count(*) from tb_zczb_bzh_info t where t.tab_id="+tab.getId()+" and t.info_type1=2";
				String sql3 = "select t.info_type2, count(*) from tb_zczb_bzh_info t where t.tab_id="+tab.getId()+" group by t.info_type2";
				t1 = this.utilService.getObjectBySql(sql1);
				t2 = this.utilService.getObjectBySql(sql2);
				tList = this.utilService.getObjectBySql(sql3, 0, 0);
			}
			Document   document = DocumentHelper.createDocument();
			Element tree = document.addElement("tree");
			tree.addAttribute("id", "0");
			Element type1= tree.addElement("item");
			if(!"0".equals(t1)){
				type1.addAttribute("text","基础管理（<font color='red'>"+t1+"</font>）");
			}else{
				type1.addAttribute("text","基础管理");
			}
			type1.addAttribute("id", "1");
			type1.addAttribute("im0", "folderClosed.gif");
			type1.addAttribute("im1", "folderOpen.gif");
			type1.addAttribute("im2", "folderClosed.gif");
			type1.addAttribute("open", "1");
			
			Element type2= tree.addElement("item");			
			if(!"0".equals(t2)){
				type2.addAttribute("text","现场管理（<font color='red'>"+t2+"</font>）");
			}else{
				type2.addAttribute("text","现场管理");
			}
			type2.addAttribute("id", "2");
			type2.addAttribute("im0", "folderClosed.gif");
			type2.addAttribute("im1", "folderOpen.gif");
			type2.addAttribute("im2", "folderClosed.gif");
			type2.addAttribute("open", "1");
			
			List allList = (List)this.utilService.getHqlQuery("from TbZczbBzhType t where 1=1 and t.typeGrade=2 order by t.typeSort");
			if(null!=allList && allList.size()>0){
				for(int i=0;i<allList.size();i++){
					boolean b = false;
					String num = "";
					TbZczbBzhType type = (TbZczbBzhType)allList.get(i);
					if(null!=tList && tList.size()>0){
						for(int j=0;j<tList.size();j++){
							Object obj[] = (Object[])tList.get(j) ;
							if(type.getId().toString().equals(obj[0].toString())){
								b = true;
								num = obj[1].toString();
								break;
							}
						}
						if("1".equals(type.getTypeParentId().toString())){
							Element main= type1.addElement("item");
							if(b){
								main.addAttribute("text",type.getTypeName()+"（<font color='red'>"+num+"</font>）");
							}else{
								main.addAttribute("text",type.getTypeName()+"（0）");
							}
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}else{
							Element main= type2.addElement("item");			
							if(b){
								main.addAttribute("text",type.getTypeName()+"（<font color='red'>"+num+"</font>）");
							}else{
								main.addAttribute("text",type.getTypeName()+"（0）");
							}
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}
					}else{
						if("1".equals(type.getTypeParentId().toString())){
							Element main= type1.addElement("item");			
							main.addAttribute("text",type.getTypeName());
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}else{
							Element main= type2.addElement("item");			
							main.addAttribute("text",type.getTypeName());
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}
					}
				}
			}
			return document;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("XzzfAction 中 checkItemInit()方法："+e.getMessage());
		}
		return null;
	}

	public String getNowMonth() {
		return nowMonth;
	}

	public void setNowMonth(String nowMonth) {
		this.nowMonth = nowMonth;
	}

	public String getZcDate() {
		return zcDate;
	}

	public void setZcDate(String zcDate) {
		this.zcDate = zcDate;
	}

	public String getYhFxTime() {
		return yhFxTime;
	}

	public void setYhFxTime(String yhFxTime) {
		this.yhFxTime = yhFxTime;
	}

	public String getYhGzTime() {
		return yhGzTime;
	}

	public void setYhGzTime(String yhGzTime) {
		this.yhGzTime = yhGzTime;
	}

	public String getYhGzType() {
		return yhGzType;
	}

	public void setYhGzType(String yhGzType) {
		this.yhGzType = yhGzType;
	}

	public String getYhGzZrr() {
		return yhGzZrr;
	}

	public void setYhGzZrr(String yhGzZrr) {
		this.yhGzZrr = yhGzZrr;
	}

	public String getYhIsZg() {
		return yhIsZg;
	}

	public void setYhIsZg(String yhIsZg) {
		this.yhIsZg = yhIsZg;
	}

	public String getYhJkcs() {
		return yhJkcs;
	}

	public void setYhJkcs(String yhJkcs) {
		this.yhJkcs = yhJkcs;
	}

	public String getYhQk() {
		return yhQk;
	}

	public void setYhQk(String yhQk) {
		this.yhQk = yhQk;
	}

	public String getYhWcTime() {
		return yhWcTime;
	}

	public void setYhWcTime(String yhWcTime) {
		this.yhWcTime = yhWcTime;
	}

	public String getYhXcyy() {
		return yhXcyy;
	}

	public void setYhXcyy(String yhXcyy) {
		this.yhXcyy = yhXcyy;
	}

	public String getYhYxfw() {
		return yhYxfw;
	}

	public void setYhYxfw(String yhYxfw) {
		this.yhYxfw = yhYxfw;
	}

	public String getYhZrrTel() {
		return yhZrrTel;
	}

	public void setYhZrrTel(String yhZrrTel) {
		this.yhZrrTel = yhZrrTel;
	}

	public String getYhType() {
		return yhType;
	}

	public void setYhType(String yhType) {
		this.yhType = yhType;
	}

	public TbZczbBzhInfoView getBzhInfo() {
		return bzhInfo;
	}

	public void setBzhInfo(TbZczbBzhInfoView bzhInfo) {
		this.bzhInfo = bzhInfo;
	}

	public String getBgdPersonnel() {
		return bgdPersonnel;
	}

	public void setBgdPersonnel(String bgdPersonnel) {
		this.bgdPersonnel = bgdPersonnel;
	}

	public String getBack() {
		return back;
	}

	public void setBack(String back) {
		this.back = back;
	}
	
	public String getScrollTop() {
		return scrollTop;
	}

	public void setScrollTop(String scrollTop) {
		this.scrollTop = scrollTop;
	}

}
